/*******************************************************************************
*  file    : rpcclient.hpp
*  created : 18.08.2013
*  author  : 
*******************************************************************************/

#ifndef RPCCLIENT_HPP
#define RPCCLIENT_HPP

#include <QObject>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <archivedb.hpp>
#include <utils.hpp>
#include <vector>
#include <array>

class QTimer;

class RpcClient : public QObject
{
    Q_OBJECT
public:
    ~RpcClient();
    static RpcClient* instance();
    const std::vector<MoleLine>&     moleList    () const {return mole_list_     ;}
    const std::vector<IncomingLine>& lastIncoming() const {return last_incoming_ ;}
    const std::vector<MoleTask>&     moleTasks   () const {return mole_tasks_    ;}
    const QString&                   modemStatus () const {return modem_status_  ;}
public slots:
    void loadSettings     ();

    void echo             (const QVariant& v);
    void getModemStatus   ();
    void addMole          (const QString& phone,
                           const QString& name,
                           const QString& passwd,
                           const QString& memo,
                           const QString& bat_change);
    void updateMole       (const QString& phone,
                           const QString& name,
                           const QString& passwd,
                           const QString& memo,
                           const QString& bat_change,
                           int            id);
    void getMoleList      ();
    void hideMoles        (const QStringList& lst);
    void addSmsToOutgoing (int idx, const QString& txt);
    void sendInfoRequest  (const QStringList& lst);
    void sendPosRequest   (const QStringList& lst);
    void sendModeRequest  (const QStringList& lst, int mode);

    void getLastIncoming  (const QStringList& lst);
    void getWorkingTasks  (const QStringList& lst);
private:
    explicit RpcClient(QObject *parent = 0);
    void     setServerState(bool);
private slots:
    void testConnection();
    void onError           (const QString& e);
    void onEcho            (const QVariant& v);
    void onModemStatus     (const QVariant& s);
    void onAddMole         (const QVariant& v);
    void onGetMoleList     (const QVariant& v);
    void onAddSmsToOutgoing(const QVariant& v);
    void onRequest         (const QVariant& v);
    void onGetLastIncoming (const QVariant& v);
    void onGetWorkingTasks (const QVariant& v);
signals:
    void moleListUpdated     (int);
    void serverStateChanged  (bool);
    void lastIncomingUpdated (int);
    void workingTasksUpdated (int);
    void modemStatusChanged  (const QString&);
private:
    QString address_;
    quint16 port_;
private:
    std::vector<MoleLine>     mole_list_;
    std::vector<IncomingLine> last_incoming_;
    std::vector<MoleTask>     mole_tasks_;
    bool                  server_connected_;
    QTimer*               test_conn_timer_;
    QString               modem_status_;
};

#endif // RPCCLIENT_HPP
